package com.szh.cloud.user.control;

import com.alibaba.fastjson.JSON;
import com.szh.cloud.mybatis.entity.PtUserEntity;
import com.szh.cloud.mybatis.flexigrid.FlexResEntity;
import com.szh.cloud.mybatis.flexigrid.PagingBean;
import com.szh.cloud.mybatis.ztree.ZtreeEntity;
import com.szh.cloud.spring.annotation.LogPoint;
import com.szh.cloud.spring.control.BaseController;
import com.szh.cloud.user.entity.PasswordEntity;
import com.szh.cloud.user.entity.SearchUserEntity;
import com.szh.cloud.user.entity.UserEntity;
import com.szh.cloud.user.execption.RepeatEmailException;
import com.szh.cloud.user.execption.RepeatMobileException;
import com.szh.cloud.user.execption.RepeatUserNameException;
import com.szh.cloud.user.execption.UseNameCcupdException;
import com.szh.cloud.user.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    @RequestMapping("/queryUserInfo")
    @ResponseBody
    public String queryUserInfo(String userName){
        List<PtUserEntity> list =  userService.queryUserInfo(userName);
        return JSON.toJSONString(list);
    }
    @RequestMapping("/deleteUserById")
    @ResponseBody
    public String deleteUserById(@RequestParam("userUuids") String userUuids){
        userService.deleteUserById(userUuids);
        return successJSON("user.delete.success");


    }
    @Secured("ROLE_cldk_data_user")
    @RequestMapping("/userPage")
    public String initPage(){
        return "user/userPage";
    }





    @RequestMapping("/getUserInfoListByPage")
    @ResponseBody
    public FlexResEntity<PtUserEntity> getUserInfoListByPage(SearchUserEntity entity, PagingBean paging){

        FlexResEntity<PtUserEntity> flexResEntity = userService.getUserInfoListByPage(entity,paging);
        return flexResEntity;
    }
    @RequestMapping("/getRoleByUserId")
    @ResponseBody
    public String getRoleByUserId(@RequestParam("userUuids") String userUuids){

        List<ZtreeEntity>  list = userService.getRoleByUserId(userUuids);

        return JSON.toJSONString(list);
    }

    @RequestMapping("/saveRoleByUser")
    @ResponseBody
    public String saveRoleByUser(@RequestParam("userUuids") String userUuids,@RequestParam("roleIds") String roleIds){

        userService.saveRoleByUser(userUuids,roleIds);
        return successJSON("user.allocat.role.success");
    }

    @RequestMapping("/getRoleBySingUserId")
    @ResponseBody
    public String getRoleBySingUserId(@RequestParam("userUuid") String userUuid){

        List<ZtreeEntity> list = userService.getRoleBySingUserId(userUuid);
        return JSON.toJSONString(list);
    }

    @RequestMapping("/addUserInfo")
    @ResponseBody
    public String addUserInfo(UserEntity userEntity){
        try {
            userService.addUserInfo(userEntity);
        } catch (RepeatUserNameException e) {
            return failJSON("user.user.name.repeat");
        } catch (RepeatEmailException e) {
//            e.printStackTrace();
            return failJSON("user.email.repeat");
        } catch (RepeatMobileException e) {
//            e.printStackTrace();
            return failJSON("user.mobile.repeat");
        }
        return successJSON("user.add.user.success");

    }

    @RequestMapping("/queryUserListById")
    @ResponseBody
    public String queryUserListById(@Param("userUuid") String userUuid ){
        PtUserEntity ptUserEntity = userService.queryUserListById(userUuid);
        return JSON.toJSONString(ptUserEntity);
    }

    @RequestMapping("/modifyUserInfo")
    @ResponseBody
    public String modifyUserInfo(UserEntity userEntity){
        try {
            userService.modifyUserInfo(userEntity);
        } catch (RepeatUserNameException e) {
            return failJSON("user.user.name.repeat");
        } catch (RepeatEmailException e) {
//            e.printStackTrace();
            return failJSON("user.email.repeat");
        } catch (RepeatMobileException e) {
//            e.printStackTrace();
            return failJSON("user.mobile.repeat");
        }

        return successJSON("user.modify.success");
    }

    @RequestMapping("/deleteUserRecycleById")
    @ResponseBody
    public String deleteUserRecycleById(@RequestParam("userUuids") String userUuids){
        try {
            userService.deleteUserRecycleById(userUuids);
        } catch (UseNameCcupdException e) {
            e.printStackTrace();
            return failJSON("user.name.occupied");
        }
        return successJSON("user.delete.success");


    }

    @RequestMapping("/modifyPasswordById")
    @ResponseBody
    public String modifyPasswordById(PasswordEntity entity){
        userService.modifyPasswordById(entity);
        return successJSON("user.password.reset.success");
    }
}


